import Vue from 'vue'
import Router from 'vue-router'


Vue.use(Router)


const routers = [
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/index.vue')
    },
    {
        path: '/',
        name: 'home',
        component: () => import('@/layout/index.vue'),
        children: [
            {
                path: '/chat',
                name: 'chat',
                component: () => import('@/views/chat/index.vue')
            },
            {
                path: '/friend',
                name: 'friend',
                component: () => import('@/views/friend/index.vue')
            },
            {
                path: '/circle',
                name: 'circle',
                component: () => import('@/views/circle/index.vue')
            },
            {
                path: '/group',
                name: 'group',
                component: () => import('@/views/group/index.vue')
            },
            {
                path: '/addGroup',
                name: 'addGroup',
                component: () => import('@/views/group/add/index.vue')
            },
            {
                path: '/expression',
                name: 'expression',
                component: () => import('@/views/admin/expression/index.vue')
            },
            {
                path: '/tool',
                name: 'tool',
                component: () => import('@/views/admin/tool/index.vue')
            }
        ]
    }
]

//加载路由
const router = new Router({
    routes: routers,
    base: '/im',
    mode: 'history',
})

//路由守卫
router.beforeEach((to, from, next) => {
    let token = sessionStorage.getItem("token")
    if (to.name !== 'login') {
        if (!token) {
            next({name: "login"})
        } else {
            next()
        }
    } else {
        next()
    }
})

export default router